package oop2;

public class BanJi {
    String name;
    String teacher;
    String[] stus = new String[30];
    int index = 0; //当前人数

    //加新生,保证新名字与数组里的已有学生姓名不重复
    public void addNewStu(String name) {
        System.out.println("添加新同学:");
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] != null && stus[i].equals(name)) { //防止空指针异常
                System.out.println("名字重复,不能添加");
                return;
            }
            if (stus[i] == null) {
                stus[i] = name;
                index++;
                return;
            }
        }
        System.out.println("班级人已满");
    }

    //班级信息
    public void showCisInfo() {
        System.out.println("班级名称:" + name + ",指导老师:" + teacher);
        System.out.println("班级学生信息为:");
        for (int i = 0; i < index; i++) {
            System.out.print(stus[i] + " ");
        }
        System.out.println();
    }

    //查找班级学生
    public boolean search(String stuName) {
        for (int i = 0; i < index; i++) {
            if (stus[i].equals(stuName)) {
                return true;
            }
        }
        return false;
    }

    //按照范围查找班级学生
    public boolean search(int start, int end, String stuName) {
        for (int i = start; i >= 0 && i <= end && i < index; i++) {
            if (stus[i].equals(stuName)) {
                return true;
            }
        }
        return false;
    }

    //改名字
    public boolean modify(String oldName, String newName) {
        for (int i = 0; i < index; i++) {
            if (stus[i].equals(oldName)) {
                stus[i] = newName;
                return true;
            }
        }
        return false;
    }

    //查找指定位置的学生姓名
    public String find(int index) {
        if (stus[index] != null)
            return "学生姓名:" + stus[index];
        else
            return "该位置为空";
    }

}
